/**
 * 
 */
package org.chat.client.screens;

import org.chat.client.model.User;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.i18n.client.Constants;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Le toolbar du chat
 * @author AGRAM Larbi
 * @version 1.1
 */
public class RichTextToolbarChat extends Composite {

	public interface Images extends ClientBundle {

		/**
		 * @ImageResource bold.gif
		 */
		@Source("org/chat/client/images/bold.gif")
		ImageResource bold();

		/**
		 * @ImageResource italic.gif
		 */
		@Source("org/chat/client/images/italic.gif")
		ImageResource italic();

		/**
		 * @ImageResource underline.gif
		 */
		@Source("org/chat/client/images/underline.gif")
		ImageResource underline();

		/**
		 * @ImageResource smile.gif
		 */
		@Source("org/chat/client/images/smileys/smile.gif")
		ImageResource smile();

		/**
		 * @ImageResource wink.gif
		 */
		@Source("org/chat/client/images/smileys/wink.gif")
		ImageResource clin();

		/**
		 * @ImageResource laughing.gif
		 */
		@Source("org/chat/client/images/smileys/laughing.gif")
		ImageResource heureux();

		/**
		 * @ImageResource yell.gif
		 */
		@Source("org/chat/client/images/smileys/yell.gif")
		ImageResource hihi();

		/**
		 * @ImageResource tongue-out.gif
		 */
		@Source("org/chat/client/images/smileys/tongue-out.gif")
		ImageResource langue();

		/**
		 * @ImageResource mechant.png
		 */
		//@Source("org/chat/client/images/smileys/mechant.png")
		//ImageResource mechant();

		/**
		 * @ImageResource surprised.gif
		 */
		@Source("org/chat/client/images/smileys/surprised.gif")
		ImageResource surprised();

		/**
		 * @ImageResource cool.gif
		 */
		@Source("org/chat/client/images/smileys/cool.gif")
		ImageResource soleil();

		/**
		 * @ImageResource frown.gif
		 */
		@Source("org/chat/client/images/smileys/frown.gif")
		ImageResource triste();
		
		/**
		 * @ImageResource folder.gif
		 */
		@Source("org/chat/client/images/folder.gif")
		ImageResource upload();

	}

	public interface Strings extends Constants {

		String black();

		String blue();

		String bold();

		String color();

		String font();

		String green();

		String italic();

		String large();

		String medium();

		String normal();

		String red();

		String size();

		String small();

		String underline();

		String white();

		String xlarge();

		String xsmall();

		String yellow();

		String smile();

		String heureux();

		String clin();

		String hihi();

		String langue();

		String mechant();

		String rire();

		String soleil();

		String triste();
		
		String  upload();
	}

	private class EventListener implements ClickHandler, ChangeHandler,
			KeyUpHandler {

		public void onClick(ClickEvent event) {

			Widget sender = (Widget) event.getSource();

			final PanelSmielys simplePopup = new PanelSmielys(richText);
			UploadFichier uploadPopup = new UploadFichier(user,ChatId);
			int left = sender.getAbsoluteLeft() + 20;
			int top = sender.getAbsoluteTop() + 20;
			
			
			if (sender == bold) {
				basic.toggleBold();
			} else if (sender == italic) {
				basic.toggleItalic();
			} else if (sender == underline) {
				basic.toggleUnderline();
			} else if (sender == smile) {
				
				simplePopup.setPopupPosition(left, top);
				
				smile.setDown(true);
				simplePopup.show();

			} else if (sender == upload) {
				
				uploadPopup.setPopupPosition(left, top);
				upload.setDown(false);
				uploadPopup.show();

			} else if (sender == richText) {
				updateStatus();
			}

		}

		public void onChange(ChangeEvent event) {

			Widget sender = (Widget) event.getSource();
			if (sender == foreColors) {
				basic.setForeColor(foreColors.getValue(foreColors
						.getSelectedIndex()));
				foreColors.setSelectedIndex(0);
			} else if (sender == fonts) {
				basic.setFontName(fonts.getValue(fonts.getSelectedIndex()));
				fonts.setSelectedIndex(0);
			} else if (sender == fontSizes) {
				basic.setFontSize(fontSizesConstants[fontSizes
						.getSelectedIndex() - 1]);
				fontSizes.setSelectedIndex(0);
			}
		}

		public void onKeyUp(KeyUpEvent event) {

			Widget sender = (Widget) event.getSource();

			if (sender == richText) {
				updateStatus();
			}

		}
	}

	private static final RichTextArea.FontSize[] fontSizesConstants = new RichTextArea.FontSize[] {
			RichTextArea.FontSize.X_SMALL, RichTextArea.FontSize.SMALL,
			RichTextArea.FontSize.MEDIUM, RichTextArea.FontSize.LARGE,
			RichTextArea.FontSize.X_LARGE };

	private Images images = (Images) GWT.create(Images.class);
	private Strings strings = (Strings) GWT.create(Strings.class);
	private EventListener listener = new EventListener();

	private RichTextArea richText;
	private RichTextArea.Formatter basic;

	private VerticalPanel outer = new VerticalPanel();
	private HorizontalPanel topPanel = new HorizontalPanel();
	private HorizontalPanel bottomPanel = new HorizontalPanel();
	private ToggleButton bold;
	private ToggleButton italic;
	private ToggleButton underline;
	private ToggleButton smile;
	private ToggleButton upload;
	private ListBox foreColors;
	private ListBox fonts;
	private ListBox fontSizes;
	
	private String ChatId;
	private User user;

	/**
	 * Creates a new toolbar that drives the given rich text area.
	 * 
	 * @param richText le rich text area a controler 
	 */
	protected RichTextToolbarChat(RichTextArea richText,User user,String ChatId) {
		this.user=user;
		this.ChatId=ChatId;
		this.richText = richText;
		this.basic = richText.getFormatter();
		richText.getFormatter();

		outer.add(topPanel);
		outer.add(bottomPanel);
		topPanel.setWidth("100%");
		bottomPanel.setWidth("100%");

		initWidget(outer);
		setStyleName("gwt-RichTextToolbar");

		if (basic != null) {
			topPanel.add(bold = createToggleButton(images.bold(), strings.bold()));
			topPanel.add(italic = createToggleButton(images.italic(), strings.italic()));
			topPanel.add(underline = createToggleButton(images.underline(), strings.underline()));
			topPanel.add(smile = createToggleButton(images.smile(), strings.smile()));
			topPanel.add(upload = createToggleButton(images.upload(), strings.upload()));
		}

		if (basic != null) {
			topPanel.add(foreColors = createColorList("Couleur"));
			topPanel.add(fonts = createFontList());
			topPanel.add(fontSizes = createFontSizes());

			richText.addKeyUpHandler(listener);
			richText.addClickHandler(listener);
		}
	}

	private ListBox createColorList(String caption) {
		ListBox lb = new ListBox();
		lb.addChangeHandler(listener);
		lb.setVisibleItemCount(1);

		lb.addItem(caption);
		lb.addItem(strings.white(), "white");
		lb.addItem(strings.blue(), "blue");
		lb.addItem(strings.yellow(), "yellow");
		lb.addItem(strings.black(), "black");
		lb.addItem(strings.red(), "red");
		lb.addItem(strings.green(), "green");
		return lb;
	}

	private ListBox createFontList() {
		ListBox lb = new ListBox();
		lb.addChangeHandler(listener);
		lb.setVisibleItemCount(1);

		lb.addItem(strings.font(), "");
		lb.addItem(strings.normal(), "");
		lb.addItem("Times New Roman", "Times New Roman");
		lb.addItem("Arial", "Arial");
		lb.addItem("Courier New", "Courier New");
		lb.addItem("Georgia", "Georgia");
		lb.addItem("Trebuchet", "Trebuchet");
		lb.addItem("Verdana", "Verdana");
		return lb;
	}

	private ListBox createFontSizes() {
		ListBox lb = new ListBox();
		lb.addChangeHandler(listener);
		lb.setVisibleItemCount(1);

		lb.addItem(strings.size());
		lb.addItem(strings.xsmall());
		lb.addItem(strings.small());
		lb.addItem(strings.medium());
		lb.addItem(strings.large());
		lb.addItem(strings.xlarge());
		return lb;
	}

	

	/**
	 * 
	 * @param img
	 * @param tip
	 * @return
	 */
	private ToggleButton createToggleButton(ImageResource img,
			String tip) {
		ToggleButton tb = new ToggleButton(new Image(img));
		tb.addClickHandler(listener);
		tb.setTitle(tip);
		return tb;
	}

	/**
	 * 
	 */
	private void updateStatus() {
		if (basic != null) {
			bold.setDown(basic.isBold());
			italic.setDown(basic.isItalic());
			underline.setDown(basic.isUnderlined());
		}

	}
}
